package com.kaichaohulian.baocms.activity;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.base.BaseActivity;
import com.kaichaohulian.baocms.http.HttpUtil;
import com.kaichaohulian.baocms.http.Url;
import com.kaichaohulian.baocms.utils.DBLog;
import com.kaichaohulian.baocms.view.ShowDialog;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import static com.kaichaohulian.baocms.activity.RegisterActivity.isMobile;

/**
 * 忘记密码界面
 * Created by ljl on 2016/12/11.
 */
public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener{
    private EditText et_usertel, et_code, et_pwd, et_newPwd;
    private ImageView iv_back;
    private Button btn_register;
    private Timer mTimer = null;
    private int mTime = 60;
    private TextView get_code;
    //标题
    private TextView tv_title;


    private ImageView img_hide;
    private ImageView img_hide2;
    private ImageView img_hide3;
    private ImageView img_hide4;
    @Override
    public void setContent() {
        setContentView(R.layout.forgetpwd_activity);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        et_usertel = getId(R.id.et_usertel);
        et_code = getId(R.id.et_code);
        et_pwd = getId(R.id.et_pwd);
        et_newPwd = getId(R.id.et_newPwd);
        iv_back = getId(R.id.iv_back);

        btn_register = getId(R.id.btn_register);
        get_code = getId(R.id.get_code);
        tv_title = getId(R.id.center_title_tv);
        tv_title.setText("忘记密码");
        img_hide = getId(R.id.img_hide);
        img_hide.setOnClickListener(this);
        img_hide2=getId(R.id.img_hide2);
        img_hide2.setOnClickListener(this);
        img_hide3 = getId(R.id.img_hide3);
        img_hide3.setOnClickListener(this);
        img_hide4=getId(R.id.img_hide4);
        img_hide4.setOnClickListener(this);
    }

    @Override
    public void initEvent() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_usertel.addTextChangedListener(new TextChange());
        et_code.addTextChangedListener(new TextChange());
        et_pwd.addTextChangedListener(new TextChange());
        et_newPwd.addTextChangedListener(new TextChange());

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!et_pwd.getText().toString().trim().equals(et_newPwd.getText().toString().trim())){
                    Toast.makeText(ForgetPwdActivity.this,"两次密码不一致，请重新输入",Toast.LENGTH_LONG).show();
                }else {

                    ShowDialog.showDialog(getActivity(), "修改中...", false, null);

                    String usertel = et_usertel.getText().toString().trim();
                    String code = et_code.getText().toString().trim();
                    String pwd = et_pwd.getText().toString().trim();
                    String newPwd = et_newPwd.getText().toString().trim();

                    RequestParams params = new RequestParams();
                    params.put("phoneNumber", usertel);
                    params.put("code", code);
                    params.put("password", pwd);
                    params.put("newPassword", newPwd);
                    HttpUtil.post(Url.forgetPassword, params, new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            try {

                                DBLog.e("修改密码：", response.toString());
                                showToastMsg(response.getString("errorDescription"));
                                if(response.getString("code").equals("0")){
                                    finish();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                ShowDialog.dissmiss();
                            }
                        }

                        @Override
                        public void onFinish() {

                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                            showToastMsg("请求服务器失败");
                            DBLog.e("tag", statusCode + ":" + responseString);
                            ShowDialog.dissmiss();
                        }
                    });
                }


            }
        });
        get_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(et_usertel.getText().toString().trim())) {
                    Toast.makeText(ForgetPwdActivity.this, "手机号码不能为空", Toast.LENGTH_LONG).show();
                }else if (!isMobile(et_usertel.getText().toString().trim())){
                    Toast.makeText(ForgetPwdActivity.this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
                }else {

                    if (mTimer == null) {
                        mTimer = new Timer();
                        mTime = 60;
                        get_code.setText(mTime + "s后重新获取");
                        get_code.setEnabled(false);
                    } else {
                        return;
                    }

                    mTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    mTime--;
                                    get_code.setText(mTime + "s后重新获取");
                                    if (mTime == 0) {
                                        mTimer.cancel();
                                        mTimer = null;
                                        get_code.setText("重新验证");
                                        get_code.setEnabled(true);
                                    }
                                }
                            });
                        }
                    }, 0, 1000);


                    String phone = et_usertel.getText().toString().trim();
                    ShowDialog.showDialog(getActivity(), "发送中...", false, null);
                    RequestParams params = new RequestParams();
                    params.put("phoneNumber", phone);
                    HttpUtil.post(Url.sendMessage, params, new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            try {
                                DBLog.e("发送验证码：", response.toString());
                                showToastMsg(response.getString("errorDescription"));
                            } catch (Exception e) {
                                showToastMsg("发送失败");
                                mTimer.cancel();
                                mTimer = null;
                                get_code.setText("重新验证");
                                get_code.setEnabled(true);
                                e.printStackTrace();
                            } finally {
                                ShowDialog.dissmiss();
                            }
                        }

                        @Override
                        public void onFinish() {

                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                            showToastMsg("请求服务器失败");
                            DBLog.e("tag", statusCode + ":" + responseString);
                            ShowDialog.dissmiss();
                        }
                    });
                }

                }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_hide:
                et_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                img_hide2.setVisibility(View.VISIBLE);
                img_hide.setVisibility(View.GONE);
                et_pwd.setSelection(et_pwd.getText().length());
                break;
            case R.id.img_hide2:
                img_hide2.setVisibility(View.GONE);
                img_hide.setVisibility(View.VISIBLE);
                et_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                et_pwd.setSelection(et_pwd.getText().length());
                break;
            case R.id.img_hide3:
                et_newPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                img_hide4.setVisibility(View.VISIBLE);
                img_hide3.setVisibility(View.GONE);
                et_newPwd.setSelection(et_newPwd.getText().length());
                break;
            case R.id.img_hide4 :
                img_hide4.setVisibility(View.GONE);
                img_hide3.setVisibility(View.VISIBLE);
                et_newPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                et_newPwd.setSelection(et_newPwd.getText().length());
                break;
        }


    }

    // EditText监听器
    class TextChange implements TextWatcher {
        @Override
        public void afterTextChanged(Editable arg0) {
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void onTextChanged(CharSequence cs, int start, int before, int count) {
            boolean Sign2 = et_usertel.getText().length() >= 11;
            boolean Sign3 = et_pwd.getText().length() >= 6;
            boolean Sign4 = et_code.getText().length() >= 6;
            boolean Sign5 = et_newPwd.getText().length() >= 6;
            if (mTimer == null) {
                if (Sign2) {
                    get_code.setEnabled(true);
                } else {
                    get_code.setEnabled(false);
                }
            }
            if (Sign2 & Sign3 & Sign4 & Sign5) {
                btn_register.setEnabled(true);
            } else {
                btn_register.setEnabled(false);
            }
        }
    }

    public void back(View view) {
        finish();
    }
}
